package com.wsz.system.controller;

import com.wsz.common.Result;
import com.wsz.system.pojo.Menu;
import com.wsz.system.service.MenuService;
import com.wsz.vo.AssginMenuVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "菜单管理")
@RestController
@RequestMapping("/admin/system/sysMenu")
public class SysMenuController {

    @Autowired
    private MenuService menuService;

    @ApiOperation("菜单列表")
    @GetMapping("findNodes")
    public Result findNodes(){
        List<Menu> list = menuService.findsNodes();
        return Result.ok(list);
    }

    @ApiOperation("根据Id查询菜单")
    @GetMapping("findMenuById/{id}")
    public Result findMenuById(@PathVariable String id){
        Menu byId = menuService.getById(id);
        return Result.ok(byId);
    }

    @ApiOperation("增加菜单")
    @PostMapping("/addMenu")
    public Result addMenu(@RequestBody Menu menu){
        boolean save = menuService.save(menu);
        if(save) return Result.ok();
        else return Result.fail();
    }

    @ApiOperation("根据Id删除菜单")
    @DeleteMapping("removeMenu/{id}")
    public Result removeMenu(@PathVariable String id){
        boolean b = menuService.removeMenuById(id);
        if(b)  return Result.ok();
        else return Result.fail();
    }

    @ApiOperation("修改菜单")
    @PostMapping("updateMenu")
    public Result updateMenu(@RequestBody Menu menu){
        boolean b = menuService.updateById(menu);
        if(b)  return Result.ok();
        else return Result.fail();
    }

    @ApiOperation("根据角色Id查询分配菜单")
    @GetMapping("/toAssign/{roleId}")
    public Result toAssign(@PathVariable String roleId){
        List<Menu> list = menuService.findMenuByRoleId(roleId);
        return Result.ok(list);
    }

    @ApiOperation("给角色分配菜单权限")
    @PostMapping("/doAssign")
    public Result doAssign(@RequestBody AssginMenuVo assginMenuVo) {
        menuService.doAssign(assginMenuVo);
        return Result.ok();
    }

}
